package com.example.yydm666.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yydm666.entity.Product;
import com.example.yydm666.entity.User;
import com.example.yydm666.mapper.ProductMapper;
import com.example.yydm666.service.CartService;
import com.example.yydm666.service.ProductCategoryService;
import com.example.yydm666.service.ProductService;
import com.example.yydm666.vo.DataVO;
import com.example.yydm666.vo.PieVO;
import com.example.yydm666.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-03-28
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private CartService cartService;

    //    商品列表
    @GetMapping("/list/{type}/{id}")
    public ModelAndView list(@PathVariable("type") String type, @PathVariable("id") Integer id, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("productList");
        modelAndView.addObject("productList", productService.findByCategoryId(type, id));
        modelAndView.addObject("list", this.productCategoryService.buildProductCategoryMenu());
        User user = (User) session.getAttribute("user");
        if (user == null) {
            //未登录
            modelAndView.addObject("cartList", new ArrayList<>());
        } else {
            //登录用户
            //查询该用户的购物车记录
            modelAndView.addObject("cartList", this.cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }

    @GetMapping("/findById/{id}")
    public ModelAndView findById(@PathVariable("id") Integer id, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("productDetail");
        modelAndView.addObject("list", this.productCategoryService.buildProductCategoryMenu());
        modelAndView.addObject("product", productService.getById(id));
        User user = (User) session.getAttribute("user");
        if (user == null) {
            //未登录
            modelAndView.addObject("cartList", new ArrayList<>());
        } else {
            //登录用户
            //查询该用户的购物车记录
            modelAndView.addObject("cartList", this.cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }


    //    后台获取全部的商品
    @RequestMapping("/allProduct")
    @ResponseBody
    public List<Product> allProduct() {
        List<Product> list = productService.list();
        return list;
    }

    //    后台分页
    @RequestMapping("/page/{page}/{size}")
    @ResponseBody
    public Page<Product> page(@PathVariable("page") Integer page,
                              @PathVariable("size") Integer size) {
        Page<Product> page1 = new Page<>(page, size);
        Page<Product> page2 = productService.page(page1, null);
        return page2;
    }

    //    后台删除
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Integer deleteById(@PathVariable("id") Integer id) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getId, id);
        int delete = productMapper.delete(wrapper);
        return delete;
    }

    //    后台修改
    @PutMapping("/update/{id}")
    @ResponseBody
    public int updateById(@RequestBody Product product, @PathVariable("id") Integer id) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getId, id);
        int update = productMapper.update(product, wrapper);
        return update;
    }

    //    后台添加
    @PostMapping("/insert")
    @ResponseBody
    public int addProduct(@RequestBody Product product) {
        int insert = productMapper.insert(product);
        return insert;
    }

    //    后台查询
    @GetMapping("/getSelect")
    @ResponseBody
//    required来调节是否必须传
    public List<Product> getSelect(
//            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "name") String name) {
        System.out.println("name = " + name);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Product::getName, name);
//               .or()
//               .like(Product::getId, id);
        List<Product> list = productService.list(wrapper);
        return list;
    }

    //    后台柱状图
    @RequestMapping("selectProductVO")
    @ResponseBody
    public ProductVO productVO() {
        ProductVO productVO = new ProductVO();
        List<String> names = new ArrayList<>();
        List<DataVO> stocks = new ArrayList<>();
//        先查询出数据
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.le(Product::getId, 738);
        List<Product> list = productService.list(wrapper);
        for (Product product : list) {
            names.add(product.getName());
            DataVO dataVO = new DataVO();
            dataVO.setValue(product.getStock());
            stocks.add(dataVO);
        }
        productVO.setNames(names);
        productVO.setStocks(stocks);
//        转换为vo
        return productVO;
    }

    //    后台饼状图
    @RequestMapping("/pieVOList")
    @ResponseBody
    public List<PieVO> pieVOList() {
        List<PieVO> pieVOList = new ArrayList<>();
        //        先查询出数据
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.le(Product::getId, 738);
        List<Product> products = productService.list(wrapper);
        for (Product product: products){
            PieVO pieVO = new PieVO();
            pieVO.setName(product.getName());
            pieVO.setValue(product.getStock());
            pieVOList.add(pieVO);
        }
        return pieVOList;
    }


}

